<?php

namespace app\index\controller;
use PHPMailer\PHPMailer\PHPMailer;
use think\Session;

use think\Db;
use think\Controller;
use think\Request;

// require './vendor/autoload.php';


class Login extends Controller
{

	// public $email;

    public function index()
    {
        $list = Db::name('huser')->where(['email' => input('post.email'),'userpass'=>md5(input('post.pwd'))])->find();
        
        if ($list > 0) {
             Session::set('id',$list['id']);
//             Session::set('homeemail',input('post.email'));
             return $this->success('登录成功', url('index/index/index'));
        } else {
            return $this->error('登录失败');
        }
        return view('index/index');
    }

   public function email()
   {
       $get = input('get.email');

       $email = rand('000000','999999');
       Session::set('EMAIL',$email);
      // return $get;
    if($get){
           
    	    $mail=new PHPMailer();
            try{
                $mail->SMTPDebug = 2;           // 邮件调试模式
                $mail->isSMTP();                // 设置邮件使用SMTP
                $mail->Host = 'smtp.163.com';   // 设置邮件程序以使用SMTP
                $mail->CharSet='UTF-8';         // 设置邮件内容的编码
                $mail->SMTPAuth = true;         // 启用SMTP验证
                $mail->Username = 'lyl940503@163.com';   // SMTP username
                $mail->Password = 'abcd1234';   // SMTP password
                $mail->SMTPSecure = 'ssl';      // 启用TLS加密，`ssl`也被接受
                $mail->Port = 465;              // 连接的TCP端口
                $mail->setFrom('lyl940503@163.com', 'Mailer');  //设置发件人
                $mail->addAddress($get, 'qq');       //  添加收件人1
                // $mail->addAddress('ellen@example.com');          // 收件人2
                // $mail->addReplyTo('fajian@aliyun.com', 'fajian');//抄送
                // $mail->addCC('cc@example.com');
                // $mail->addBCC('bcc@example.com');

                //附件
               // $mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
               // $mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name
                //Content
                $mail->isHTML(true);                    // 将电子邮件格式设置为HTML
                $mail->Subject = '邮箱注册';
                $mail->Body    = '邮箱验证码'.$email;
  //            $mail->AltBody = '这是非HTML邮件客户端的纯文本';
                $mail->send();
                return "数据成功";

            }catch (Exception $e){
                echo 'Mailer Error: ' . $mail->ErrorInfo;
            }
        }else{ return 'cc'; }
     }
    
     public function register()
     {
//        dump(Session::get());die;
        if(input('post.yzm') != Session::get('EMAIL')){
                return $this->error('验证码错误');
            }
         $data = [
            'name' => input('post.name'),
            'email' => input('post.email'),
            'userpass' => md5(input('post.pwd'))

        ];

        // var_dump($data);die;
        $result = Db::name('huser')->data($data)->insert();

        if ($result > 0) {
             return $this->success('注册成功', url('index/index/index'));
        } else {
            return $this->error('注册失败');
        }

     }

     public function out()
     {
       Session::delete('homeemail','think');
       Session::delete('id','think');
       $this->redirect('index/index/index');
     }
}
